﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Domain.Abstract;
using Web.Domain.Entities;

namespace Web.UI.Controllers
{
    public class CompoController : BaseController
    {
        private IComponentRepository repository;
        private IDisplayRepository disrepo;
        public CompoController(IDisplayRepository drepo, IComponentRepository repo)
        {
            repository = repo;
            disrepo = drepo;
        }
        public ActionResult Index()
        {
            return View(repository.Components);
        }
        public ActionResult Create()
        {
            return View("Editor", new Component());
        }
        public ActionResult Editor(int id)
        {
            Component compo = repository.Components.FirstOrDefault(c => c.id == id);
            return View(compo);
        }
        [HttpPost]
        public ActionResult Editor(Component compo)
        {
            if (ModelState.IsValid)
            {

                if (repository.SaveCompo(compo))
                {
                    showSuccess(compo.Name.ToString() + " 保存成功");
                }
                else
                {
                    showError(compo.Name.ToString() + " 保存失败");
                }
                return RedirectToAction("Index");
            }
            else
            {
                return View(compo);
            }
        }
        public ActionResult Delete(int id)
        {
            Component delete = repository.DeleteCompo(id);
            if (delete != null)
            {
                showSuccess(delete.Name + " 已经删除");
            }
            return RedirectToAction("Index");
        }

        //显示方式
        public ActionResult dAciton(int id)
        {
            return View(disrepo.Displays.Where(i => i.id == id));
        }

        public ActionResult CreateDis()
        {
            return View("EditorDis", new Display());
        }

        public ActionResult EditorDis(int id)
        {
            Display display = disrepo.Displays.FirstOrDefault(i => i.id == id);
            return View(display);
        }
    }
}
